// pages/question/question.js
const {myfetch}=require("../../utils/util")
const app=getApp()
Page({
 
    /**
     * 页面的初始数据 
     */
    data: {
        type:'',
        color1:'',
        color2:'',
        color:'',
        scrollTop:'500rpx',
        path:'',
        isSelect:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
        let query=wx.createSelectorQuery()
        query.select('#editor').context(res=>{
            this.editor=res.context;
        }).exec()
    },
    click:function(e){
        if(e.currentTarget.dataset.text=='账号问题'){
            this.setData({
                type:e.currentTarget.dataset.text,
                color1:'red',
                color2:'',
                color3:''
            })
        }else if(e.currentTarget.dataset.text=='功能问题'){
            this.setData({
                type:e.currentTarget.dataset.text,
                color1:'',
                color2:'red',
                color3:''
            })
        }else{
            this.setData({
                type:e.currentTarget.dataset.text,
                color1:'',
                color2:'',
                color3:'red'
            })
        } 
    },
    selectImage:async function(){
        const that=this
        if(this.data.isSelect==false){
            let res=await new Promise((rv,rj)=>{
                wx.chooseImage({
                    count:1,
                    sizeType:['compressed'],
                    success:res=>{
                        that.setData({
                            isSelect:true
                        })
                        rv(res)
                        this.editor.insertImage({
                            src:res.tempFilePaths[0],
                        })
                        this.bottom()
                    },
                    fail:res=>{
                        console.log(res)
                    }
                })
            })
            let r= await new Promise((rv,rj)=>{
                wx.uploadFile({
                    // name是上传名，不是文件名，把本地文件上传到该网址
                    filePath:res.tempFilePaths[0],
                    name:'image',
                    url:'https://wx.xxmtcds.top:2333/user/image',
                    header:{
                        authorization:app.globalData.token
                    },
                    dataType:'text',
                    success:res=>{
                        console.log(res)
                        that.setData({
                            path:res.data
                        })
                    }
                })
            })
        }else{
            wx.showModal({
                title:'提示',
                content:'每次提交只能上传一张照片哦！'
            })
        }
        
    },
    bottom:function(){
        this.setData({
            scrollTop:10000
        })
    },
    editorPost:function(f){ 
        const that=this
        let {number} = f.detail.value;
        this.editor.getContents({
            success:res=>{ 
                if(number==''){
                    wx.showModal({
                        title:'提示',
                        content:'请输入联系方式'
                    })
                    
                }else if(this.data.type==''){
                    wx.showModal({
                        title:'提示',
                        content:'请选择反馈类型'
                    })
                }else{
                    console.log(this.data.type)
                    let arr=res.delta.ops;
                    console.log(arr)
                    let brr=[];
                    let obj={
                        text:'',
                        image:''
                    }
                    let container=''
                    for(let i=0;i<arr.length;i++){
                        brr[i]=arr[i].insert;
                        
                    }
                    console.log(number)
                    console.log(brr)
                    for(let i=0;i<brr.length;i++){
                        if(brr[i].image){
                            obj.image=that.data.path
                            continue;
                        }
                        obj.text+=brr[i]
                    }
                    console.log(obj)
                    wx.request({
                        url:'https://wx.xxmtcds.top:2333/user/feedback',
                        method:'POST',
                        dataType:'text',
                        header:{
                            authorization:app.globalData.token
                        },
                        data:{
                            type:that.data.type,
                            content:obj,
                            number:number
                        },
                        success:res=>{
                            console.log(res)
                            if(res.statusCode==200){
                                wx.showModal({
                                    title:'提示',
                                    content:'提交成功！'
                                })
                            }
                        }
                    })
                }
            },
            fail:err=>{
                console.log(err)
            }
        })
    },


    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let style=wx.getStorageSync('style')
        if(style!=''){
            this.setData({ 
                contentStyle:wx.getStorageSync('style')
            })
        }else{
            this.setData({
                contentStyle:'https://pic.imgdb.cn/item/6199ad962ab3f51d91e21dd0.jpg'
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})